<template>
  <div>
    <FilterTable
      ref="searchCoustomerTable"
      :columns="columnsPart"
      columnsRef="addsearchCoustomerTable"
      :fetchapi="fetchapi"
      :params="fetchParamsPart"
      selectionType="single"
      :datakey="'rows'"
      height="300"
      :onRowSelectChange="columns => (this.partColumns = columns)"
      :onColumnsChange="columns => (this.columns = columns)"
    />
    <div style="text-align:center;">
      <el-button size="small" type="primary" @click="saveDialog">确定</el-button>
      <el-button size="small" type="primary" @click="closePage">取消</el-button>
    </div>
  </div>
</template>

<script>
import { queryStockInfo } from '@/api/repair/repairAssign/repairAssign';
import { getChexSelect } from '@/api/repair/repairReception/repairBusinessManger';
import { dictionary } from '@/utils/dictMixin';
import { findStorageCode } from '@/api/repair/basicParameters';
export default {
  name: 'addpartModel',
  mixins: [dictionary],
  data() {
    return {
      fetchParamsPart: {},
      partColumns: [],
      fetchapi: queryStockInfo,
      topFilterList: this.createTopFilterList(),
      columnsPart: this.createTableColumnsPart()
    };
  },
  mounted() {
    this.BaseTable = this.$refs.partModelTable.$refs.pageTable;
  },
  created() {
    this.getStorage();
  },
  methods: {
    createTableColumnsPart() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库代码',
          dataIndex: 'storageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型组',
          dataIndex: 'partModelGroupCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '拼音代码',
          dataIndex: 'spellCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件类别',
          dataIndex: 'partGroupCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单位',
          dataIndex: 'unitCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库存数量',
          dataIndex: 'stockQuantity',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售价',
          dataIndex: 'salesPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '索赔价',
          dataIndex: 'claimPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售限价',
          dataIndex: 'limitPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '成本单价',
          dataIndex: 'costPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '成本金额',
          dataIndex: 'costAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '最大库存',
          dataIndex: 'maxStock',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '最小库存',
          dataIndex: 'minStock',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '借进数量',
          dataIndex: 'borrowQuantity',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '借出数量',
          dataIndex: 'lendQuantity',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '最新入库日期',
          dataIndex: 'lastStockIn',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '最新出库日期',
          dataIndex: 'lastStockOut',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    async getStorage() {
      const res = await findStorageCode();
      this.topFilterList.find(item => item.fieldName === 'storageCode').itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
    },
    saveDialog() {
      this.$emit('add', this.partColumns);
    },
    changeHandle(val) {
      this.fetchParamsPart = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async getSeries(id) {
      this.topFilterList.find(item => item.fieldName === 'brand').itemList = (await getChexSelect(id)).data.map(x => ({ text: x.SERIES_NAME, value: x.SERIES_CODE }));
    },
    // 品牌下拉框改变时
    changePinpSelect(id) {
      this.brandCode = id;
      if (!id) return;
      this.getSeries(id);
    },
    closePage() {
      this.$emit('close');
    }
  }
};
</script>
